package com.aoxon.cargo.component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.District;

public class WordScrollView extends ScrollView {

	private Context context;
	private LayoutInflater inflater;
	private Map<String, Integer> wordPosition = new HashMap<String, Integer>();
	private List<District> list;
	private LinearLayout contentView;
	private OnClickListener onClick = null;
	private boolean showHot;
	public View onClickedView = null;

	private int num = 0;

	public WordScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	public WordScrollView(Context context) {
		super(context);
		this.context = context;
		inflater = LayoutInflater.from(context);
		// TODO Auto-generated constructor stub
	}

	public void setUp(List list, LinearLayout contentView) {
		this.contentView = contentView;
		this.list = list;
		notifyDataChange();
	}

	public void setItemOnClick(OnClickListener onClick) {
		this.onClick = onClick;
	}

	// public WordScrollView(List list,Context context,LinearLayout
	// contentView){
	// this.context = context;
	// inflater = LayoutInflater.from(context);
	// this.contentView = contentView;
	// this.list = list;
	// }

	public void scrollToWord(String word) {
		int position = 0;
		try {
			Log.v("word", word);
			while (true) {
				if (wordPosition.containsKey(word)) {
					position = wordPosition.get(word);
					break;
				} else {
					char c = word.charAt(0);
					c++;
					word = String.valueOf(c);
					if (c > 'Z') {
						break;
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			position = 0;
		}
		int height = contentView.getHeight();
		int toY = (int) ((double) (position / (double) num) * height);
		this.scrollTo(0, toY);
		Log.v("scroll view scroll to Y:", String.valueOf(toY));

	}

	public void notifyDataChange() {
		contentView.removeAllViews();
		String word = "";
		int position = 0;
		//加载热门城市出来
		if(isShowHot()){
			for(int i = 0 ; i< list.size() ; i++){
				
				if(i == 0){
					contentView.addView(getGrayView(position,"热门城市"));
					position++;
				}
				District dis = list.get(i);
				if(dis.getHotCity() == 1){
					contentView.addView(getView(dis));
					position++;
				}
			}
		}
		
		for (int i = 0; i < list.size(); i++) {
			if (!word.equals(list.get(i).getFirstLetter())) {
				word = list.get(i).getFirstLetter();
				contentView.addView(getWordView(position, word));
				position++;
				contentView.addView(getView(list.get(i)));
				position++;
			} else {
				contentView.addView(getView(list.get(i)));
				position++;
			}
		}
		num = position;
	}

	private View getGrayView(int position, String word) {

		View view = inflater.inflate(R.layout.gray_text_item, null);
		TextView tvItem = (TextView) view.findViewById(R.id.tvItem_gray);
		tvItem.setText(word);
		view.setTag("word");
		return view;
	}

	private View getWordView(int position, String word) {

		String format_word = String.valueOf(word.charAt(0)).toUpperCase();
		View view = inflater.inflate(R.layout.gray_text_item, null);
		TextView tvItem = (TextView) view.findViewById(R.id.tvItem_gray);
		tvItem.setText(format_word);
		view.setTag("word");
		wordPosition.put(format_word, position);
		return view;
	}

	private View getView(District dis) {
		View view = inflater.inflate(R.layout.item_search_sup, null);
		TextView tvItem = (TextView) view.findViewById(R.id.tvItem_search_sup);
		tvItem.setText(dis.getDistrict());
		view.setTag(dis);
		if (onClick != null) {
			view.setOnClickListener(onClick);
		}
		return view;
	}
	
	public boolean isShowHot() {
		return showHot;
	}

	public void setShowHot(boolean showHot) {
		this.showHot = showHot;
	}
}
